// pages/home/richMan/richMan.js
var app = getApp;
const router = require('../../../config').router

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lastPage:0,
    pageNo:1,
    banners: [
      // {
      //   adId: '50',
      //   associateId: '1074',
      //   adName: '金陵国花园打造华东地区最大的牡丹观赏园',
      //   adCode: '111',
      //   adImage: "http://www.jsxcpd.com/ftpdata/userfile/survey/imges/20180417/20180417135627436.jpg",
      //   adDepict: '',
      //   adUrl: '',
      //   video: 'http://www.jsxcpd.com/ftpdata/userfile/survery/video/20180417/20180417133848582.mp4'
      // }
    ],
    list: [
      //{
      //   createDateStr:"2019-01-08 09:32:40",
      //   description:"",
      //   id:1152,
      //   image:"http://www.jsxcpd.com/ftpdata/userfile/survey/imges/20190108/20190108093213702.jpg",
      //   title:"《走进新农村》第240期",
      //   video:"http://www.jsxcpd.com/ftpdata/userfile/survery/video/20190108/20190108092305575.mp4",
      // }
    ],
    windowHeight: '',
    windowWidth: '',
    height: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //致富经轮播图
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'ad.query.richClassics',
        v: '1.0',
        format: 'json'
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data.ad)
          that.setData({
            banners: res.data.ad
          })
        }
      }
    }); 
    //致富经列表信息
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'article.list.richClassics',
        v: '1.0',
        format: 'json'
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data)
          that.setData({
            list: res.data.article,
            lastPage: res.data.page.last,
          })
        }
      }
    });
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    }) 
  },
  imageError(e) {
    var that = this
    var height = e.detail.height;
    var width = e.detail.width;
    var ratio = width / height
    var height1 = parseInt(that.data.windowWidth / ratio);
    console.log(height1)
    that.setData({
      height: height1 + 'px'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 详情
   */
  articleClick: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/index?id=' + id
    })
  },
  //上拉加载
  onReachBottom() {
    var that = this;
    var p = this.data.pageNo
    p++
    var last = that.data.lastPage
    if (p <= last) {
      //致富经列表信息
      wx.request({
        url: router,
        data: {
          appKey: 'w4q897jgvxkb',
          method: 'article.list.richClassics',
          v: '1.0',
          format: 'json',
          pageNo: p
        },
        success: function (res) {
          if (res.statusCode == 200 && res.data.error != 'error') {
            // console.log(res.data)
            for (var i = 0; i < res.data.article.length; i++) {
              that.data.list.push(res.data.article[i])
            }
            wx.showToast({
              title: '加载中',
              icon: 'loading',
              duration: 1000
            })
            setTimeout(function () {
              that.setData({
                list: that.data.list,
                pageNo: res.data.page.pageNo
              })
            }, 1000)
          }
        }
      }); 
    } else {
      wx.showToast({
        title: '没有更多。。',
        icon: 'loading',
        duration: 1500
      })
    }
  }
})